﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.IO;

namespace Intorise.API
{
    public class ValidateCodeHelper
    {
        int codeDatabaseHandler = 0;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath">识别库路径</param>
        public ValidateCodeHelper(string filePath)
        {
            codeDatabaseHandler = LoadCdsFromFile(filePath);
            if (codeDatabaseHandler == -1)
            {
                throw new Exception("识别库载入出错");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath">识别库路径</param>
        /// <param name="password">识别库密码</param>
        public ValidateCodeHelper(string filePath, string password)
        {
            codeDatabaseHandler = LoadCdsFromFile(filePath, password);
            if (codeDatabaseHandler == -1)
            {
                throw new Exception("识别库载入出错");
            }
        }

        /// <summary>
        /// 从图像数组获取验证码
        /// </summary>
        /// <param name="imgData"></param>
        /// <returns></returns>
        public string GetVcode(byte[] data)
        {
            StringBuilder sb = new StringBuilder();
            if (GetVcodeFromBuffer(codeDatabaseHandler, data, data.Length, sb))
            {
                return sb.ToString();
            }
            return "";
        }

        /// <summary>
        /// 从位图获取验证码
        /// </summary>
        /// <param name="imgData"></param>
        /// <returns></returns>
        public string GetVcode(Bitmap image)
        {
            StringBuilder sb = new StringBuilder();
            if (GetVcodeFromBuffer(codeDatabaseHandler, image.GetHbitmap(), sb))
            {
                return sb.ToString();
            }
            return "";
        }

        /// <summary>
        /// 从图像窗口句柄获取验证码
        /// </summary>
        /// <param name="imgData"></param>
        /// <returns></returns>
        public string GetVcode(IntPtr windowHandler)
        {
            StringBuilder sb = new StringBuilder();
            if (GetVcodeFromHWND(codeDatabaseHandler, windowHandler, 0, sb, 0))
            {
                return sb.ToString();
            }
            return "";
        }

        [DllImport("AntiVC.dll")]   //文件载入识别库（路径）
        static extern int LoadCdsFromFile(string FilePath, string password);

        [DllImport("AntiVC.dll")]   //文件载入识别库（路径）
        static extern int LoadCdsFromFile(string FilePath);

        [DllImport("AntiVC.dll")]	//通过句柄识别（识别库索引，句柄，区域[0为窗口全部,RECT类型]，结果，调试路径[正常0]）
        static extern bool GetVcodeFromHWND(int CdsFileIndex, IntPtr HWND, int lpRect, StringBuilder Vcode, int DebugMode);

        [DllImport("AntiVC.dll")]	//通过图像句柄识别 返回是否成功识别
        static extern bool GetVcodeFromBuffer(int CdsFileIndex, IntPtr HbitmapHandler, StringBuilder code);

        [DllImport("AntiVC.dll")]
        static extern int LoadCdsFromBuffer(byte[] FileBuffer, int FileBufLen, string Password);

        [DllImport("AntiVC.dll")]
        static extern bool GetVcodeFromFile(int CdsFileIndex, string FilePath, StringBuilder Vcode);

        [DllImport("AntiVC.dll")]
        static extern bool GetVcodeFromBuffer(int CdsFileIndex, byte[] FileBuffer, int ImgBufLen, StringBuilder Vcode);
    }
}
